<template>
<div class="container">
  <headers :title="title" :refresh="isRefresh" :path="path"></headers>
  <div class="content">
    <div class="list-block inset mar-bottom-10 mar-top-10">
      <ul>
        <li class="item-content">
          <div class="item-inner">
            <div class="item-title">已提现总额</div>
            <div class="item-after">{{userInfo.cashSum}}元</div>
          </div>
        </li>
        <li class="item-content">
          <div class="item-inner">
            <div class="item-title">金币数</div>
            <div class="item-after">{{userInfo.canCashGold}}</div>
          </div>
        </li>
        <li class="item-content">
          <div class="item-inner">
            <div class="item-title">可提现总额</div>
            <div class="item-after">{{userInfo.money}}元</div>
          </div>
        </li>
      </ul>
    </div>
    <div class="color-light">
      <div class="mar-left-15 size-14">每个用户每日可提现一次，即时到账</div>
    </div>
    <div class="code">
      <img :src="qrCodeUrl">
    </div>
    
    <div class="content-block">
      <div class="col-50">
        <a class="button button-big button-fill button-danger" @click="tixian">
          提现
        </a>
      </div>
    </div>
  </div>
</div>
</template>

<script>
import $ from 'zepto'
import Headers from '../components/Headers'
import store from '../assets/js/store.min.js'

export default {
  ready () {
    $.init()
    this.getUserInfo()
    this.getGenerateAuthCode()
  },
  data () {
    return {
      userInfo: [],
      qrCodeUrl: store('qrCodeUrl') || '',
      title: '提现',
      path: '/home',
      customerId: store('customerId') || 0,
      isGenerate: false,
      isRefresh: false
    }
  },
  methods: {
    getUserInfo () {
      this.$http.jsonp('http://www.mayishengcai.com/VoteManager/rest/findEnchashmentData', {
        params: {
          customerId: this.customerId
        }
      }).then((data) => {
        this.$set('userInfo', data.data)
      })
    },
    getGenerateAuthCode (isMsg, callback) {
      this.$http.jsonp('http://www.mayishengcai.com/VoteManager/rest/generateAuthCode', {
        params: {
          customerId: this.customerId
        }
      }).then((data) => {
        if (data.data.generateAuthCode) {
          $.confirm('您还没有绑定微信<br />是否立刻绑定？', () => {
            let u = navigator.userAgent
            let arr = {imgUrl: data.data.generateAuthCode}
            if (!!u.match(/AppleWebKit.*Mobile.*/) && (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1)) {
              try {
                window.Android.shareimg(arr.imgUrl)
              }
              catch (e) {
                console.warn(e)
              }
            }
            if (!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
              try {
                window.webkit.messageHandlers.shareimg.postMessage(arr)
              }
              catch (e) {
                console.warn(e)
              }
            }
          })
        }
        else {
          if (isMsg) {
            $.alert(data.data.msg)
          }
          this.$set('isGenerate', true)
        }
        if (callback) {
          callback()
        }
      })
    },
    tixian () {
      $.showIndicator()
      if (!this.isGenerate) {
        this.getGenerateAuthCode(true, () => {
          $.hideIndicator()
        })
        return false
      }
      this.$http.jsonp('http://www.mayishengcai.com/VoteManager/rest/enchashment', {
        params: {
          customerId: this.customerId,
          cashMoney: this.userInfo.money
        }
      }).then((data) => {
        $.hideIndicator()
        if (data.data.success) {
          $.alert(data.data.message, function () {
            location.reload()
          })
        }
        else {
          $.alert(data.data.message)
        }
      }, () => {
        $.hideIndicator()
        $.alert('网络异常')
      })
      return false
    }
  },
  components: {
    Headers
  }
}
</script>
<style scoped>
#cue{padding-left:0;padding-right:0;}
.code{width:100%;padding:15px 50px}
.code img{width:100%;}
</style>